package com.nowcoder.Offer.Stack;

import org.junit.Test;

import java.util.Stack;

/**
 * 用两个栈实现队列
 */
public class StackToQueue {
    // head
    Stack<Integer> stack1 = new Stack<Integer>();
    //tail
    Stack<Integer> stack2 = new Stack<Integer>();

    public void push(int node) {
        if(stack2.isEmpty())
            stack1.push(node);
        else{
            while(!stack2.isEmpty()){
                stack1.push(stack2.pop());
                stack1.push(node);
            }
        }
    }

    public int pop() {
        if(stack1.isEmpty()){
            return stack2.pop();
        }else{
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }

    @Test
    public void test(){
        StackToQueue stackToQueue = new StackToQueue();
        stackToQueue.push(1);
        stackToQueue.push(2);
        stackToQueue.push(3);
        for(int i=0; i<3; i++)
            System.out.println(stackToQueue.pop());
    }
}
